# -*- coding: utf-8 -*-

"""
DateTime   : 2021/02/09 18:17
Author     : ZhangYafei
Description: 
"""
import timeit


def list_insert_time_test():
    l = []
    for i in range(10):
        l.insert(0, i)

def list_append_time_test():
    l = []
    for i in range(10):
        l.append(i)

def list_gen_time_test():
    l = [i for i in range(10)]

def list_extend_time_test():
    l = []
    for i in range(10):
        l.extend([i])

def list_range_time_test():
    l = list(range(10))


def test_timeit_function():
    insert_time_timeit = timeit.timeit(stmt='list_insert_time_test()', setup='from __main__ import list_insert_time_test')
    print('insert_time_timeit: ', insert_time_timeit)

    append_time_timeit = timeit.timeit(stmt='list_append_time_test()', setup='from __main__ import list_append_time_test')
    print('append_time_timeit: ', append_time_timeit)


def test_timeit_expression():
    insert_time_timeit = timeit.timeit(stmt='list(insert_list.insert(0, i) for i in init_list)',
                                       setup='insert_list=list();init_list=range(10)',
                                       number=10000)
    print('insert_time_timeit: ', insert_time_timeit)
    append_time_timeit = timeit.timeit(stmt='list(append_list.append(i) for i in init_list)',
                                       setup='append_list=list();init_list=range(10)',
                                       number=10000)
    print('append_time_timeit: ', append_time_timeit)


def test_repeat_function():
    insert_time_repeat = timeit.repeat(stmt='list_insert_time_test()',
                                       setup='from __main__ import list_insert_time_test')
    print('insert_time_repeat: ', insert_time_repeat)
    append_time_repeat = timeit.repeat(stmt='list_append_time_test()',
                                       setup='from __main__ import list_append_time_test')

    print('append_time_repeat: ', append_time_repeat)


def test_repeat_expression():
    insert_time_repeat = timeit.repeat(stmt='list(insert_list.insert(0, i) for i in init_list)',
                                       setup='insert_list=list();init_list=range(10)',
                                       repeat=5,
                                       number=10000)
    print('insert_time_repeat: ', insert_time_repeat)
    append_time_repeat = timeit.repeat(stmt='list(append_list.append(i) for i in init_list)',
                                       setup='append_list=list();init_list=range(10)',
                                       repeat=5,
                                       number=10000)
    print('append_time_repeat: ', append_time_repeat)
    """
    insert_time_repeat:  [16.5112801, 17.996148199999997, 14.8008533, 14.227645800000005, 12.636173499999998]
    append_time_repeat:  [0.07276369999999588, 0.07817310000000077, 0.08558549999999343, 0.08086780000000715, 0.07534959999999558]
    """


def test_Timer_insert():
    timer_insert = timeit.Timer(stmt='list_insert_time_test()', setup='from __main__ import list_insert_time_test')
    insert_time_timeit = timer_insert.timeit(number=1000000)
    print('insert_time_timeit: ', insert_time_timeit)
    insert_time_repeat = timer_insert.repeat(number=1000000)
    print('insert_time_repeat: ', insert_time_repeat)


def test_Timer_append():
    timer_append = timeit.Timer(stmt='list_append_time_test()', setup='from __main__ import list_append_time_test')
    append_time_timeit = timer_append.timeit(number=1000000)
    print('append_time_timeit: ', append_time_timeit)
    append_time_repeat = timer_append.repeat(number=1000000)
    print('append_time_repeat: ', append_time_repeat)


def test_Timer_print():
    timer_append = timeit.Timer(stmt='time.sleep(1)', setup='import time')
    append_time_timeit = timer_append.timeit(number=10)
    print('append_time_timeit: ', append_time_timeit)
    append_time_repeat = timer_append.repeat(number=10)
    print('append_time_repeat: ', append_time_repeat)


def test_list():
    timer3 = timeit.Timer("list_gen_time_test()", "from __main__ import list_gen_time_test")
    print('[i for i in range]:', timer3.timeit(10000))
    timer4 = timeit.Timer("list_range_time_test()", "from __main__ import list_range_time_test")
    print('list(range())', timer4.timeit(10000))
    timer5 = timeit.Timer("list_extend_time_test()", "from __main__ import list_extend_time_test")
    print('extend:', timer5.timeit(10000))


if __name__ == '__main__':
    # test_timeit_function()
    # test_timeit_expression()
    # test_repeat_function()
    # test_repeat_expression()
    # test_Timer_insert()
    # test_Timer_append()
    # test_Timer_print()
    test_list()
